using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

// 克隆技能类，继承自Skill
public class Clone_Skill : Skill
{
    [Header("Clone info")]
    [Tooltip("克隆体伤害倍数")]
    [SerializeField] private float attackMultiplier;
    [Tooltip("预制克隆体")]
    [SerializeField] private GameObject clonePrefab;
    [Tooltip("克隆体持续时间")]
    [SerializeField] private float cloneDuration;
    [Space]
    [Header("克隆体攻击 Clone Attack")]
    [SerializeField] private UI_SkillTreeSlot cloneAttackUnlockButton;
    [SerializeField] private float cloneAttackMultiplier;
    [SerializeField] private bool canAttack;

    [Header("Aggresive clone")]
    [SerializeField] private UI_SkillTreeSlot aggresiveCloneUnlockButton;
    [SerializeField] private float aggresiveCloneAttackMultiplier;
    public bool canApplyOnHitEffect { get; private set; } // 造成武器效果

    [Header("克隆体克隆 Multiple clone")]
    [SerializeField] private UI_SkillTreeSlot multipleUnlockButton;
    [SerializeField] private float multiCloneAttackMultiplier;
    [SerializeField] private bool canDuplicateClone; // 幻影再幻影开关
    [SerializeField] private float chanceToDuplicate = 35; // 幻影再幻影几率

    [Header("水晶替换幻影 Crystal instead of clone")]
    [SerializeField] private UI_SkillTreeSlot crystalInseadUnlockButton;
    public bool crystalInseadOfClone; // 幻影变水晶开关 

    // 重写Start方法
    protected override void Start()
    {
        base.Start();
        // 添加解锁克隆体攻击的按钮点击事件
        cloneAttackUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockCloneAttack);
        // 添加解锁攻击性克隆体的按钮点击事件
        aggresiveCloneUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockAggresiveClone);
        // 添加解锁多重克隆体的按钮点击事件
        multipleUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockMultiClone);
        // 添加解锁水晶替换克隆体的按钮点击事件
        crystalInseadUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockCrystalInstead);
    }

    // 重写CheckUnlock方法
    protected override void CheckUnlock()
    {
        UnlockCloneAttack();
        UnlockAggresiveClone();
        UnlockMultiClone();
        UnlockCrystalInstead();
    }

    #region 技能解锁按钮

    // 解锁克隆体攻击
    private void UnlockCloneAttack()
    {
        if (cloneAttackUnlockButton.unlocked)
        {
            canAttack = true;
            attackMultiplier = cloneAttackMultiplier;
        }
    }

    // 解锁攻击性克隆体
    private void UnlockAggresiveClone()
    {
        if (aggresiveCloneUnlockButton.unlocked)
        {
            canApplyOnHitEffect = true;
            attackMultiplier = aggresiveCloneAttackMultiplier;
        }
    }

    // 解锁多重克隆体
    private void UnlockMultiClone()
    {
        if (multipleUnlockButton.unlocked)
        {
            canDuplicateClone = true;
            attackMultiplier = multiCloneAttackMultiplier;
        }
    }

    // 解锁水晶替换克隆体
    private void UnlockCrystalInstead()
    {
        if (crystalInseadUnlockButton.unlocked)
        {
            crystalInseadOfClone = true;
        }
    }

    #endregion

    // 创建克隆体
    public void CreateClone(Transform _cloneTransform, Vector3 _offset)
    {
        if (crystalInseadOfClone) // 幻影成水晶
        {
            SkillManager.instance.crystal.CreateCrystal(); // 创造水晶
            return;
        }
        // 制造克隆体
        GameObject newClone = Instantiate(clonePrefab);
        // 获取克隆体身上的脚本并设置位置
        newClone.GetComponent<Clone_Skill_Controller>().SetupClone(_cloneTransform, cloneDuration, canAttack, _offset, FindClosestEnemy(newClone.transform), canDuplicateClone, chanceToDuplicate, attackMultiplier);
    }

    // 格挡释放幻影
    public void CreateCloneWithDelay(Transform _enemyTransform)
    {
        StartCoroutine(CloneDelayCoroutine(_enemyTransform, new Vector3(2 * player.facingDir, 0)));
    }

    // 延时释放幻影
    private IEnumerator CloneDelayCoroutine(Transform _enemyTransform, Vector3 _offset)
    {
        yield return new WaitForSeconds(.4f);
        CreateClone(_enemyTransform, _offset);
    }
}